package com.example.jl.pickpiclibrary.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;


import com.example.jl.pickpiclibrary.R;
import com.example.jl.pickpiclibrary.adapter.BigPhotoViewPagerAdaper;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

import java.util.ArrayList;


public class BigPhotoShowActivity extends Activity {

    private ViewPager view_pager;
    private ArrayList<String> datas;
    private int position;
    private BigPhotoViewPagerAdaper bigPhotoViewPagerAdaper;
    private ImageLoader imageLoader;
    private ArrayList<View> views;
    int count;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_big_photo_show);
        view_pager = (ViewPager)findViewById(R.id.view_pager);

        datas = getIntent().getStringArrayListExtra("photos");
        position = getIntent().getIntExtra("position", 0);

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
                .threadPriority(Thread.NORM_PRIORITY - 2).denyCacheImageMultipleSizesInMemory()
                .diskCacheFileNameGenerator(new Md5FileNameGenerator()).diskCacheSize(100 * 1024 * 1024)
                .diskCacheFileCount(300).tasksProcessingOrder(QueueProcessingType.LIFO).build();
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(config);


        views =initListViews(datas);
        bigPhotoViewPagerAdaper = new BigPhotoViewPagerAdaper(views);
        view_pager.setAdapter(bigPhotoViewPagerAdaper);
        view_pager.setCurrentItem(position);
    }
    private ArrayList<View> initListViews( ArrayList<String>  imagepaths) {
        ArrayList<View> Views = new ArrayList<>();
        for (String imagepath:imagepaths){
            ImageView imageView = new ImageView(this);
            imageLoader.displayImage("file://" + imagepath, imageView);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));
            Views.add(imageView);
        }
        return Views;
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_big_photo_show, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.delete_pic) {

            if (views.size() == 1) {
                datas.clear();
                finish();
            } else {
                int position =view_pager.getCurrentItem();
                view_pager.removeAllViews();
                datas.remove(position);
                views.remove(position);
                bigPhotoViewPagerAdaper.notifyDataSetChanged();
            }

            return true;
        }

        return super.onOptionsItemSelected(item);
    }



    @Override
    public void finish() {
        Intent intent = new Intent();
        intent.putStringArrayListExtra("revisedPicture",datas);
        setResult(RESULT_OK,intent);

        super.finish();
    }
}
